﻿using System;
using System.Collections.Generic;
using System.Data;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Module.Core.BusinessLogic;
using Module.Core.BusinessLogic.CmsContents;
using Module.Core.Common;
using Module.Core.EntitiesInfo;
using Module.Core.LoginSystem.Commons.Framwork;
using Module.Core.EntitiesInfo.TablesInfo;

namespace Web.Cms.FQA
{       
    
    public partial class FAQcreate : AdminPage
    {   
        #region Khai báo
        private int FAQ_ID = 0;
        #endregion
        protected void Page_Load(object sender, EventArgs e)
        {
            FAQ_ID = ObjConvertUtility.ToInt32(Request.QueryString["FAQ_ID"]);
            if (!Page.IsPostBack)
            {
                LoadData();
                txtDescription.Height = 180;
                txtDescription.Width = 460;
                txtCreateOn.SelectedDate = DateTime.Now;
                CMSZonesBL.GetSub(dropZoneId, "0", 1, 0, CurrentUser.UserID);
            }
        }

        #region Thêm mới
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                FAQQuestionsInfo info = new FAQQuestionsInfo();
                FillData(info);
                int i = FAQQuestions.Insert(info);
                if (i > 0)
                {
                    lblUpdateStatus.Text = "Bạn đã thêm mới thành công";
                    ClearText();
                }
            }
            catch (Exception ex)
            {
                lblUpdateStatus.Text = "Error: " + ex.Message;
            }
        }
        #endregion




        #region FillData
        protected void FillData(FAQQuestionsInfo info)
        {
            info.CreatedOn = DateTime.Now;
            info.Answer = txtDescription.Text;
            info.Question = txtQuestion.Text;
            info.ZoneId = Convert.ToInt32(dropZoneId.SelectedValue.ToString());
            info.CreatedBy = CurrentUser.Username;
            info.FAQ_ID = FAQ_ID;
            info.Priority = 0;
            info.Priority = CheckType();
            
        }
        #endregion

                #region ClearText
        void ClearText()
        {
        
            txtQuestion.Text = "";
            txtDescription.Text = "";

        
        }
        #endregion

        #region CheckType
        int CheckType()
        {
            CheckBox[] Boxes = new CheckBox[3];
            Boxes[0] = chkCao;
            Boxes[1] = chkTrungbinh;
            Boxes[2] = chkThap;

            return ClsCheckBox.ReturnStatusCheck(Boxes);
        }
        #endregion

        #region SelectedCheckBox
        void SelectedCheckBox(FAQQuestionsInfo Info)
        {
            CheckBox[] Boxes = new CheckBox[3];
            Boxes[0] = chkCao;
            Boxes[1] = chkTrungbinh;
            Boxes[2] = chkThap;

            chkCao.Checked = ClsCheckBox.SelectChecked(ClsCheckBox.Cao, Info.Priority, Boxes);
            chkTrungbinh.Checked = ClsCheckBox.SelectChecked(ClsCheckBox.TrungBinh, Info.Priority, Boxes);
            chkThap.Checked = ClsCheckBox.SelectChecked(ClsCheckBox.Thap, Info.Priority, Boxes);
        }
        #endregion



        #region LoadData
        private void LoadData()
        {
            try
            {
                if (FAQ_ID > 0)
                {
                    btnUpdate.Visible = true;
                    btnAdd.Visible = false;
                    FAQQuestionsInfo info = FAQQuestions.Select(FAQ_ID);
                    if (info != null)
                    {
                        txtQuestion.Text = info.Question;
                        txtDescription.Text = info.Answer;
                        SelectedCheckBox(info);
                        dropZoneId.SelectedValue = info.ZoneId.ToString();
                        txtCreateOn.SelectedDate = DateTime.Now;


                    }
                }
                else
                {
                    btnUpdate.Visible = false;
                    btnAdd.Visible = true;
                    
                }
            }
            catch (Exception ex)
            {
                lblUpdateStatus.Text = "Error: " + ex.Message;
            }
        }
        #endregion

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                FAQQuestionsInfo info = new FAQQuestionsInfo();
                FillData(info);
                FAQQuestions.Update(info);
                lblUpdateStatus.Text = "Bạn đã chỉnh sửa thành công";
            }
            catch (Exception ex)
            {
                lblUpdateStatus.Text = "Error: " + ex.Message;
            }
        }
    }
}